import { NodeStatus } from "../NodeStatus";
import { ActionNode } from "./ActionNode";



/**
 * 异步动作节点基础类
 */
export abstract class AsyncActionNode<T> extends ActionNode<T> {

    private halt_requested: boolean;

    constructor(name: string, blackboard: T) {
        super(name,blackboard);
        this.halt_requested = false;
    }
        
    Evaluate(): NodeStatus {
        if (this.status === NodeStatus.IDLE) {
            this.status = NodeStatus.RUNNING;
            this.halt_requested = false;
            (async () => {
                try {
                    // this.waiting = true;
                    this.status = await this.Tick();
                    // this.waiting = false;
                } catch (error) {
                    // this.waiting = false;
                    throw new Error(error as any);
                }
            })();
        }
        return this.status;
    }

    public isHaltRequested(): boolean {
        return this.halt_requested;
    }

    Halt() {
        this.halt_requested = true;
        // while (this.waiting) {}
        // this.setStatus(NodeStatus.IDLE);
    }
}